package com.hetao.xx.service;

import com.hetao.xx.Global;
import com.hetao.xx.orm.UserVo;

import java.util.HashMap;
import java.util.Map;

public class HoleLevelService {

    class HoleLevel {
        public int upMoney;
        public int holeLevel;
        public int nextLevel;
        public float enhance;
    }

    private final int maxLevel = 10;
    private Map<Integer, HoleLevel> map;
    public HoleLevelService() {
        init();
    }

    /**
     * 初始化洞府信息
     */
    private void init() {
        map = new HashMap<>(4);
        int upMoney = 1000;
        for (int i=1; i<=maxLevel; i++) {

            HoleLevel hole = new HoleLevel();
            hole.holeLevel = i;
            if (i+1 != maxLevel) {
                hole.nextLevel = i + 1;
            } else {
                hole.nextLevel = -1;
            }
            hole.upMoney = upMoney;
            hole.enhance = (100f + i*20) / 100f;
            map.put(hole.holeLevel, hole);
            upMoney = upMoney * 3;
        }

    }

    /**
     * 洞府增强后经验
     */
    public long enhanceExperience(long experience, int holeLevel) {
        if (holeLevel == 0) {
            return experience;
        }

        HoleLevel level = map.get(holeLevel);
        if (level == null) {
            return experience;
        }
        float rst = experience * level.enhance;
        return (long)rst;
    }

    /**
     * 升级洞府
     * @param userVo
     * @return
     */
    public boolean upHoleLevel(UserVo userVo) {
        if (userVo.holeLevel == maxLevel) {
            Global.getInstant().toastMsg("等级已满，无法升级");
            return false;
        }

        HoleLevel level = map.get(userVo.holeLevel+1);
        if (level.upMoney > userVo.money) {
            Global.getInstant().toastMsg("金额不足，需要灵石："+level.upMoney);
            return false;
        }

        userVo.money -= level.upMoney;
        userVo.holeLevel = level.holeLevel;
        Global.getInstant().toastMsg("升级成功, 现在等级"+userVo.holeLevel);
        UserManager.saveAll();
        return true;
    }

}
